package com.jie.eclipse.pan.form.support;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TreeItem;

import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.utils.Utils;

public class AttributeBuilder{
	protected Hashtable arrListItem = null;
	protected ArrayList arrList = null;
	protected String strExtAttr = "";

	public synchronized void Set(String strKey, String strValue)
	{
		Set(strKey,strValue,false);
	}
	public synchronized void Set(String strKey, String strValue,boolean bIngoreValueEmpty)
	{
		if(StringHelper.Length(strKey) == 0)
			return;
		
		if(StringHelper.Length(strValue) == 0)
		{
			if(!bIngoreValueEmpty)
				return;
		}
		
		if(arrListItem == null)
		{
			arrListItem = new Hashtable();		
			arrList = new ArrayList();
		}

		arrList.remove(strKey);
		arrList.add(strKey);		
		arrListItem.put(strKey, strValue);
	}
	
	public synchronized void Append(String strKey, String strValue){
		this.Set(strKey, StringHelper.Format("%1$s %2$s",StringHelper.getValue(this.Get(strKey)),strValue));
	}

	public synchronized String Get(String strKey)
	{
		if(arrListItem == null)
			return "";
		
		if (arrListItem.containsKey(strKey))
		{
			return (String) arrListItem.get(strKey);
		}
		return "";
	}

	public synchronized void Remove(String strKey)
	{
		if(arrListItem == null)
			return;
		
		if (arrListItem.containsKey(strKey))
		{
			arrListItem.remove(strKey);
		}
		arrList.remove(strKey);
	}

	public synchronized void Clear()
	{
		if(arrListItem == null)
			return;
		
		arrListItem.clear();
	}

	public synchronized String ToOutputString()
	{
		if(arrListItem == null)
			return "";
		
		String strOutput = "";
		
		int nCount = arrList.size();
		for(int i=0;i<nCount;i++)
		{
			String strKey = (String) arrList.get(i);
			if(StringHelper.Length(strKey) == 0)
				continue;
			
			String strValue = (String) arrListItem.get(strKey);
//			if(StringHelper.Length(strValue) == 0)
//				continue;
			
			if("extAtr".equalsIgnoreCase(strKey.toLowerCase())){
				strOutput += StringHelper.Format(" %1$s",strValue);
			}else{
				strOutput += StringHelper.Format(" %1$s=\"%2$s\"", strKey, strValue.trim());
			}
		}
		
		if(!StringHelper.IsNullOrEmpty(strExtAttr))
		{
			strOutput += " ";
			strOutput += strExtAttr;
		}
		return strOutput;
	}
	
	/**
	 * 从Hashtable 初始化配置
	 * @param hashtable
	 * @param bReset
	 */
	public void InitFromHashtable(LinkedHashMap hashtable,Hashtable paramtable,boolean bReset )
	{
		if(bReset)
		{
			Clear();
		}
		
		if(hashtable == null)
			return;
		
		Iterator en = hashtable.keySet().iterator();
		while(en.hasNext())
		{
			String strKey = (String)en.next();
			String strValue = (String)hashtable.get(strKey);
			Set(this.GetParamName(strKey, paramtable),strValue,true);
		}
	}
	
	public synchronized String GetParamName(String strKey,Hashtable paramtable)
	{
		//Utils.print(strKey);
		if(paramtable == null)
			return "";
		
		if (paramtable.containsKey(strKey))
		{
			return (String) paramtable.get(strKey);
		}
		return "";
	}
	
	
	public void SetExtAttr(String strExtAttr)
	{
		this.strExtAttr = strExtAttr;
	}
}
